package com.lcz.googleplay.holder;

import android.graphics.Bitmap;
import android.text.format.Formatter;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lcz.googleplay.R;
import com.lcz.googleplay.config.BaseProtocol;
import com.lcz.googleplay.domain.DetailInfo;
import com.lcz.googleplay.holder.BaseHolder;
import com.lcz.googleplay.utils.UIUtils;
import com.yolanda.nohttp.rest.Response;
import com.yolanda.nohttp.rest.SimpleResponseListener;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hp on 2016/11/9.
 */

public class Detail_InfoHolder extends BaseHolder<DetailInfo> {

    @BindView(R.id.item_icon)
    ImageView itemIcon;
    @BindView(R.id.item_title)
    TextView itemTitle;
    @BindView(R.id.item_rating)
    RatingBar itemRating;
    @BindView(R.id.item_download)
    TextView itemDownload;
    @BindView(R.id.item_version)
    TextView itemVersion;
    @BindView(R.id.item_date)
    TextView itemDate;
    @BindView(R.id.item_size)
    TextView itemSize;

    @Override
    public View init() {
        View view = View.inflate(UIUtils.getActivity(), R.layout.detail_appinfo, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void refreshView(DetailInfo data) {
        //需要开启服务器获取图片
        String imageUri = BaseProtocol.IMAGE_URL + data.getIconUrl();
        SimpleResponseListener<Bitmap> listener = new SimpleResponseListener<Bitmap>() {
            @Override
            public void onSucceed(int what, Response<Bitmap> response) {
                super.onSucceed(what, response);
                itemIcon.setImageBitmap(response.get());
            }

            @Override
            public void onFailed(int what, Response<Bitmap> response) {
                super.onFailed(what, response);
                itemIcon.setImageResource(R.drawable.ic_default);
            }
        };
        BaseProtocol.loadImage(imageUri, listener);
        itemTitle.setText(data.getName());
        itemRating.setRating(data.getStars());
        itemDownload.setText("下载："+data.getDownloadNum());
        itemVersion.setText("版本："+data.getVersion());
        itemDate.setText("时间："+data.getDate());
        itemSize.setText("大小："+ Formatter.formatFileSize(UIUtils.getActivity(),data.getSize()));

    }
}
